package com.dq.oss.service.impl;

import com.dq.oss.service.OssService;
import com.dq.oss.utils.ConstantPropertiesUtils;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.UUID;

/**
 * @project: guli_parent
 * @ClassName: OssServiceImpl
 * @author: dq
 * @creat: 2022/7/20 11:09
 */
@Service
public class OssServiceImpl implements OssService {
    @Resource
    private ConstantPropertiesUtils constantPropertiesUtils;


    @Override
    public String uploadFileAvatar(MultipartFile file) {
        /**
         * 获取文件
         */
        try {
            COSClient cosClient = getCosClient();
            // 指定要上传的文件
            /**
             * 1. 获取流
             * 2. 拼接name  uuid 。jpg
             * 3. 设置size 节约空间
             * 4. 设置请求体
             * 5. 发送最终上传指令
             */
            InputStream inputStream = file.getInputStream();
            String filename = file.getOriginalFilename();
            String pic = UUID.randomUUID().toString().replace("-", ""); //UUID
            String picName = file.getOriginalFilename(); //文件名称 用于获取后缀名
            String nowDate = new DateTime().toString("yyyy/MM/dd");
            assert picName != null;
            // 1. /guli/   2. 2022/7/20  3. /  4. 011619125c53add6%21400x400  5. .jpg
            String picUrlNameAndType = constantPropertiesUtils.getPrefix()+nowDate +"/"+ pic + picName.substring(picName.lastIndexOf("."));
            //设置流的大小
            ObjectMetadata objectMetadata = new ObjectMetadata();
            objectMetadata.setContentLength(file.getSize());
            //设置对象
            PutObjectRequest putObjectRequest = new PutObjectRequest(constantPropertiesUtils.getBucketName(), picUrlNameAndType, inputStream, objectMetadata);
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            //关闭
            cosClient.shutdown();

            //统一返回 此路径规则：https://guli-dq-1310347558.cos.ap-chengdu.myqcloud.com/guli/011619125c53add6%21400x400.jpg
            String ResultUrl = constantPropertiesUtils.getUrl() + picUrlNameAndType;
            return ResultUrl;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 初始化CosClient相关配置， appid、accessKey、secretKey、region
     *
     * @return
     */
    public COSClient getCosClient() {
        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(constantPropertiesUtils.getSecretId(), constantPropertiesUtils.getSecretKey());
        ClientConfig clientConfig = new ClientConfig(new Region(constantPropertiesUtils.getRegion()));
        // 3 生成cos客户端
        COSClient cosClient = new COSClient(cred, clientConfig);
        return cosClient;
    }


}
